package com.javaweb.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @desc:
 * @author:
 * @date:
 */

/**
 * TODO:
 * 1. 对所有Dao中select方法进行日志输出，日志需要包含 函数名字
 * 2. 对所有Dao中select方法进行执行时间日志输出，日志需要包含 函数名字及执行时间。
 *
 *
 */

@Aspect
@Component
@Slf4j
public class LogAspect {

    /**
     * point cut
     */
    @Pointcut("execution(public * com.javaweb.dao..*.*(..))")
    public void daoLayerAllMethods(){} // 用一个空方法当 point Cut变量

    @Pointcut("execution(* com.javaweb.dao..*.insert*(..))")
    public void daoLayerMethodsStartWithInsert(){}
    /**
     * advices
     */
//    @Before("daoLayerAllMethods()")
//    public void simpleLogBeforeMethod(JoinPoint jp) {
//
//        log.info("method: "+ jp.getSignature().getName()+" was ready for execution.") ;
//
//    }

//    @After("daoLayerMethodsStartWithInsert()")
//    public void afterAllInsertMethods(JoinPoint jp) {
//        log.info("method: " + jp.getSignature().getName() + " was invoked no matter" +
//                " whether its calling was normal nor not.") ;
//    }

//    @Around("daoLayerAllMethods()")
//    public Object measurePerformanceForAllDaoMethods(
//            ProceedingJoinPoint pjp) {
//        /**
//         * before method execution, set a starting time.
//         */
//        Long start = System.currentTimeMillis() ;
//        String methodName = pjp.getSignature().getName() ;
//        Object ret = null ;
//        try {
//            ret = pjp.proceed();
//        } catch (Throwable e) {
//            log.error("method: " + methodName
//                    + " met an error: " + e.getMessage()) ;
//        }
//        /**
//         * after its execution, set an ending time-point.
//         */
//        Long end = System.currentTimeMillis() ;
//        /*
//          log for performance.
//         */
//        log.info("method: " + methodName
//                + ", spent (s): " + (end - start) / 1000.0) ;
//        return ret ;
//
//
//    }


}
